package com.qf.service.impl;

import com.qf.api.CacheFegin;
import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("fee")
@Slf4j
public class FeeFilterServiceImpl implements FilterChainService {

    @Autowired
    private PushMsgUtil pushMsgUtil;
    @Autowired
    private CacheFegin cacheFegin;

    /**
     * 扣费策略
     * @param standardSubmit
     * @return
     * @throws Exception
     */
    @Override
    public boolean strategy(StandardSubmit standardSubmit) throws Exception {

        //查询 费用和客户账户余额
        //客户余额
        Object fee = cacheFegin.hget(CacheConstants.CACHE_PREFIX_CLIENT+standardSubmit.getClientID(), "fee");
        Long p=standardSubmit.getFee();
        //短信费用
        String price = (String) cacheFegin.hget(CacheConstants.CACHE_PREFIX_ROUTER + ":"
                + standardSubmit.getClientID() + ":"
                + standardSubmit.getGatewayID(), "price");

            boolean flag = true;
            if (fee instanceof Long){ //防止类型转换异常 instanceof
                Long f= (Long) fee;
                flag = compare(f,p);
            }else if (fee instanceof Integer){
                Integer f=(Integer) fee;
                flag = compare(f,p);
            }else if (fee instanceof String){
                String f=(String) fee;
                flag = compare(f,p);
            }
            if (flag){
                return true;
            }


        return false;
    }

    //客户剩余金额比较
    public boolean compare (Long f,Long l){
        System.out.println("==Long==");
        //long l=Long.parseLong(price);
        if (f>=l){
            return true;
        }else{
            return false;
        }

    }
    //客户剩余金额比较
    public boolean compare (Integer f,Long l){
        System.out.println("==Integer==");
        if (f>=l){
            return true;
        }else{
            return false;
        }
    }
    //客户剩余金额比较
    public boolean compare (String f,Long l){
        System.out.println("==String==");
        Integer ff=Integer.parseInt(f);
        if (ff>=l){
            return true;
        }else{
            return false;
        }
    }
}
